package org.hredu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Mysql {
	private Connection connection=null;	
	private Statement statement=null;
	ResultSet resultSet=null;
	public ResultSet getResultSet() {
		return resultSet;
	}
	public void setResultSet(ResultSet resultSet) {
		this.resultSet = resultSet;
	}
	public Statement getStatement() {
		return statement;
	}
	public void setStatement(Statement statement) {
		this.statement = statement;
	}
	public Connection getConnection() {
		try {
			Config config = new Config();
			String dbUrl = "jdbc:mysql://"+config.getIp()+":"+config.getPort()+"/"+config.getDatabase();
			String dbClass = "com.mysql.jdbc.Driver";
			Class.forName(dbClass);
			connection = DriverManager.getConnection(dbUrl,config.getUser(),config.getPass());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return connection;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	public Mysql(){	}
	public void checkConnection(){
		try {
			if(connection.isClosed()) 
				this.getConnection();
		} catch (Exception e) {
			// TODO: handle exception
		}		
	}
	public int getNumRowsTable(String tableName){
		 int i = 0;
		 resultSet=null;
		 try {
			 this.checkConnection();
			 statement = connection.createStatement();
			 resultSet = statement.executeQuery("select count(*) from "+tableName);
			 while(resultSet.next()){
				i = resultSet.getInt(1); 
			 }
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return i;
	 }
	 
	 public int getMaxValue(String tableName, String colName){
		 int i = 0;
		 try {
			 ResultSet resultSet=null;
			 this.checkConnection();
			 statement = connection.createStatement();
			 resultSet = statement.executeQuery("select max("+colName+") from "+tableName);
			 while(resultSet.next()){
				i = resultSet.getInt(1); 
			 }
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return i;
	 }
	 
	 public ResultSet executeQueryRS(String sql){
		 statement=null;
		 resultSet=null;
		 try {
			 this.checkConnection();
			 statement = connection.createStatement();
			 resultSet = statement.executeQuery(sql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		 return resultSet;
	 }
	 
	 public int executeQueryUpdate(String sql){
		 int i=-1;
		 try {
			this.checkConnection();
			statement = connection.createStatement();
			i = statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO: handle exception
		}
		 return i;
	 }
	 
	 public static void main(String[] a){
		 try {
			 Mysql mysql = new Mysql();
			 mysql.getConnection();
			 ResultSet rs = mysql.executeQueryRS("select * from login");
			 while (rs.next()) {
				 System.out.println(rs.getString(1) + " " + rs.getString(2));
			  }
			 System.out.println(mysql.getNumRowsTable("login"));
			 //System.out.println(mysql.getMaxValue("admin","id"));
		} catch (Exception e) {
			// TODO: handle exception
		}
	 }
}
